#include "Ogre.h"
#include "OgrePlatform.h"

#include "GameApp.h"
#include "GameInfo.h"
#include "GameEngine.h"

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
	try
	{
		CGameApp* app = CGameApp::Singleton();
		app->init();
		Ogre::Singleton<Ogre::Root>::getSingleton().startRendering();
		app->fini();
	}
	catch (Ogre::Exception& e)
	{
		MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
		std::cerr << "An exception has occurred: " << e.getFullDescription().c_str() << std::endl;
	}

	return 0;
}
#endif // OGRE_PLATFORM == OGRE_PLATFORM_WIN32

CGameApp::CGameApp()
{
	mGameInfo = NULL;
	mGameEngine = NULL;
}

CGameApp::~CGameApp()
{
}

bool CGameApp::init()
{
	mGameEngine = new CGameEngine();
	mGameEngine->initEngine(getGameName());
	initResource();
	mGameEngine->initResource();
	onEngineInited();
	return true;
}

void CGameApp::fini()
{
	mGameEngine->finiEngine();
	delete mGameEngine;
}

CGameInfo* CGameApp::setGameInfo(CGameInfo* game)
{
	CGameInfo* old = mGameInfo;
	if(old) old->onUnload();
	mGameInfo = game;
	if(game) game->onLoad();
	return old;
}

void CGameApp::initResource()
{
}

void CGameApp::onEngineInited()
{
}
